package com.equipment;

import com.util.EncodingUtil;

/**
 * OBU卡片命令通道AC
 *
 * @version 1.18
 * @author 刘亚东
 *
 */
public class OBUCardCommandChannelRequest {
	// 55AA000002FDAC020500B095002B05805C00020485
	private String STX = "55AA"; // 帧开始标志
	private String RSCTL = "00"; // 串口帧序列号
	private String Length = "";// 长度
	private String code = "FD"; // 代码
	private String broadening = "AC"; // 扩展代码
	private String BCC = ""; // 异或校验值

	private String Number = ""; // 重发次数 20
	private String Interval = "";// 重发间隔 15
	private String ChannelType = ""; // 1:OBU中的卡进行操作，2:对OBU中的ESAM进行操作
	private String APDULIST = ""; // APDU命令个数(长度1)
	@SuppressWarnings("unused")
	private String Apdu1Len = ""; // APDU1命令长度(长度1)
	private String Apdu1Cmd = ""; // Apdu1命令(长度N)

	/**
	 * 发送AC指令保持OBU不掉电
	 *
	 * @param apdulist
	 * @param apdu1cmd
	 * @return
	 */
	public String command(String number, String interval, String type,
						  String... apdu1cmd) {
		this.Number = EncodingUtil.desToHex(Integer.parseInt(number), 2);
		this.Interval = EncodingUtil.desToHex(Integer.parseInt(interval), 2);
		this.ChannelType = EncodingUtil.addZero(type, 2);
		this.APDULIST = EncodingUtil.desToHex(apdu1cmd.length, 2);

		Apdu1Cmd = "";
		for (int i = 0; i < apdu1cmd.length; i++) {
			Apdu1Cmd += apdu1cmd[i];
		}
		// 计算长度
		Length = EncodingUtil.desToHex((broadening + Number + Interval
				+ ChannelType + APDULIST + Apdu1Cmd).length() / 2, 4);
		// 异或校验从RSCTL到DATA所有字节的异或值
		String DATA = RSCTL + Length + code + broadening + Number + Interval
				+ ChannelType + APDULIST + Apdu1Cmd;
		// 异或校验从RSCTL到DATA所有字节的异或值
		BCC = EncodingUtil.yihuo(EncodingUtil.change(DATA));
		// 拼接完整的请求
		String retn = STX + DATA + BCC;
		return retn;
	}

}
